package com.cpr.core.rabbitmq.client.pubsub.defaultex;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

/**
 * @author Chen Panrui
 * created at 2022/3/15 15:00
 *
 * 默认交换器
 * exchange交换器可以为空，默认的exchangeBindType=Direct,此时routeKey=队列的名称
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // 指定协议 用户名+密码+host+端口号+虚拟主机
        factory.setUri("amqp://admin:123456@106.13.89.7:5672/%2f");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        // exclusive:是否只能当前connection使用
        channel.queueDeclare("queue.default.ex", false, false, false, null);
        // 使用默认的exchange，路由键=队列名称
        channel.basicPublish("", "queue.default.ex", null, "Draven".getBytes(StandardCharsets.UTF_8));

        channel.close();
        connection.close();
    }
}
